package com.example.sss.service.UserService;

import com.example.sss.dao.UserMapper;
import com.example.sss.model.domin.User;
import com.example.sss.model.utils.ObsPage;
import com.example.sss.service.ObsService.ObsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.List;

@Transactional
@Service
public class UserServiceImpl implements UserService{
    //依赖注入
    @Autowired
    UserMapper userMapper;
    @Autowired
    ObsService obsService;

    @Override
    public User login(User user) {
        User user1 = new User();
        user.setPassword(md5Encode(user.getPassword()));
        user = userMapper.login(user);
        if(user==null){
            user1.setStaus(false);
        }else{
            user1=user;
            user1.setStaus(true);
        }
        return user1;
    }

    @Override
    public ObsPage selectUsers(User user) {
        PageHelper.startPage(user.getCurPage(),user.getPageSize());
        Page<User> users =(Page<User>) userMapper.selectUsersList(user);
        return new ObsPage(users.getTotal(),users.getResult());
    }
    /**
     *
     * @param user
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addUser(User user) {
        User user1=new User();
        user1.setPassword(md5Encode(user.getPassword()));
        user1.setUserId(user.getUserId());
        //判断用户名是否重复
        List<User> users =userMapper.selectUsersList(user1);
        if(users.size()<1){
            userMapper.addUser(user);
            //为用户创建桶
            obsService.createBucket(user.getId());
            return 1;
        }else{
            return 2;
        }
    }

    @Override
    public void changeState(User user) {
        userMapper.updateState(user);
    }

    @Override
    public int uppsw(User user) {
        user.setPassword(md5Encode(user.getPassword()));
        return userMapper.uppsw(user);
    }

    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteUserById(id);
        //删除该用户的桶
        obsService.deleteBucket(id);
    }

    @Override
    public int upemail(User user) {
        return userMapper.upemail(user);
    }

    @Override
    public int forgetpwd(User user) {
        String password = user.getPassword();
        user.setPassword(null);
        List<User> user1 =userMapper.selectUsersList(user);
        if (user1.size()>0){
            user.setPassword(md5Encode(password));
            userMapper.forgetpwd(user);
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public int upphone(User user) {
        return userMapper.upphone(user);
    }

    @Override
    public List<User> selectUsersByUserid(Integer userId) {
        return userMapper.selectUsersByUserid(userId);
    }

    /**
     * <p>
     * md5加密
     * </p>
     * 
     * @author Ric 2021/04/14 18:19
     */
    public String md5Encode(String inStr){
        MessageDigest md5 = null;
        try{
            md5 = MessageDigest.getInstance("MD5");
        }catch(Exception e){
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }
        byte[] byteArray = null;
        try {
            byteArray = inStr.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for(int i=0;i<md5Bytes.length;i++){
            int val = md5Bytes[i] & 0xff;
            if(val<16){
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }
}
